package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DUnitExcution;
import com.fortune.dto.in.dUnit.DUnitAddDto;
import com.fortune.dto.in.dUnit.DUnitEditDto;
import com.fortune.dto.in.dUnit.DUnitListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DUnitEnum;
import com.fortune.service.DUnitService;
import com.fortune.vo.dUnit.DUnit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "dProduct", description = "经销商商品单位管理", tags = {"经销商商品单位管理相关"})
@RestController
@RequestMapping("dUnit")
public class DUnitController {
    @Autowired
    private DUnitService dUnitService;

    @ApiOperation(value = "商品单位列表", nickname = "dUnitList", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<DUnit>> list(HttpServletRequest request, @ApiIgnore @Validated DUnitListDto listDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        listDto.setPage((listDto.getPage()-1)*10);
        List<DUnit> list = dUnitService.queryDunit(did,listDto);
        int count = dUnitService.queryDunitCount(did,listDto);
        PagingData<DUnit> pagingData = new PagingData<>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DUnitExcution<PagingData<DUnit>>(pagingData, DUnitEnum.SUCCESS);
    }

    @ApiOperation(value = "添加商品单位", nickname = "dUnitAdd", notes = "" ,tags = {"@石匡"})
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DUnitAddDto addDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        int num  = dUnitService.queryDunitExist(did,addDto);
        if (num > 0){
            return new DUnitExcution<Void>(null,DUnitEnum.EXIST);
        }
        num = dUnitService.addDunit(did,addDto);
        if (num < 1){
            return new DUnitExcution<Void>(null,DUnitEnum.FAILURE);
        }
        return new DUnitExcution<Void>(null, DUnitEnum.SUCCESS);
    }

    @ApiOperation(value = "编辑商品单位", nickname = "dUnitEdit", notes = "" ,tags = {"@石匡"})
    @PutMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DUnitEditDto editDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        int num  = dUnitService.queryEditDunitExist(did,editDto);
        if (num > 0){
            return new DUnitExcution<Void>(null,DUnitEnum.EXIST);
        }
        num = dUnitService.editDunit(did,editDto);
        if (num < 1){
            return new DUnitExcution<Void>(null,DUnitEnum.FAILURE);
        }
        return new DUnitExcution<Void>(null, DUnitEnum.SUCCESS);
    }

    @ApiOperation(value = "删除商品单位", nickname = "dUnitDelete", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParam(name="dUnitId", value="商品单位id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="商品单位id不能为空") String dUnitId) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        int num = dUnitService.deleteDunit(did,dUnitId);
        if (num < 1){
            return new DUnitExcution<Void>(null,DUnitEnum.FAILURE);
        }
        return new DUnitExcution<Void>(null, DUnitEnum.SUCCESS);
    }
}
